package org.jiucheng.magpiebridge.server.aio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.TimeUnit;

public class ServerCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, ServerAttachment> {

    public void completed(AsynchronousSocketChannel result, ServerAttachment attachment) {
        attachment.getServer().accept(attachment, this);
        
        ByteBuffer readBuffer = ByteBuffer.allocate(13);
        result.read(readBuffer, Server.READ_TIMEOUT, TimeUnit.SECONDS, new ClientAttachment(result).setReadBuffer(readBuffer), new ClientCompletionHandler());
    }

    public void failed(Throwable exc, ServerAttachment attachment) {
        exc.printStackTrace();
    }
}
